/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter.Listeners;

import com.SyDeck.sdCities.CityInfo;
import com.SySammy.SdBountyHunter.Handlers.PrisonHandler;
import com.SySammy.SdBountyHunter.sdLaw;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class PrisonMaker {

    public PrisonMaker(sdLaw plugin) {
        this.plugin = plugin;
    }
    sdLaw plugin;
    PrisonHandler PH;

    public void RegPrisonOwner(Player p, boolean server) {
        PH = new PrisonHandler(plugin);
        CityInfo ci = new CityInfo();
        String CityName = ci.GetCityName(p.getName());
        PH.RegPrisonOwner(p, CityName, server);
        p.sendMessage(ChatColor.GREEN + "Prison info logged, please left click on the ground location.");
    }

    public boolean CanLeftClickL(Player p) {
        PH = new PrisonHandler(plugin);
        return PH.CanLeftClickL(p.getName());
    }

    public boolean CanLeftClickS(Player p) {
        PH = new PrisonHandler(plugin);
        return PH.CanLeftClickS(p.getName());
    }

    public boolean CanLeftClickLC(Player p) {
        PH = new PrisonHandler(plugin);
        return PH.CanLeftClickLC(p.getName());
    }

    public boolean CanLeftClickRC(Player p) {
        PH = new PrisonHandler(plugin);
        return PH.CanLeftClickRC(p.getName());
    }

    public void RegPrisonLocation(Player p, Location l) {
        PH = new PrisonHandler(plugin);
        PH.RegPrisonLoc(p.getName(), l.getWorld().getName(), l.getX(), l.getY(), l.getZ());
        p.sendMessage(ChatColor.GREEN + "Prison location logged, please left click on the sign.");
    }

    public void RegSign(Player p, Location l) {
        PH = new PrisonHandler(plugin);
        PH.RegPrisonSign(p.getName(), l.getWorld().getName(), l.getX(), l.getY(), l.getZ());
        p.sendMessage(ChatColor.GREEN + "Prison sign logged, please left click on the Left Chest.");
    }

    public void RegChestL(Player p, Location l) {
        PH = new PrisonHandler(plugin);
        PH.RegPrisonLC(p.getName(), l.getWorld().getName(), l.getX(), l.getY(), l.getZ());
        p.sendMessage(ChatColor.GREEN + "Prison Left Chest logged, please left click on the Right Chest.");
    }

    public void RegChestR(Player p, Location l) {
        PH = new PrisonHandler(plugin);
        boolean RegPrisonRC = PH.RegPrisonRC(p.getName(), l.getWorld().getName(), l.getX(), l.getY(), l.getZ());
        if (RegPrisonRC == false) {
            p.sendMessage(ChatColor.RED + "Your right chest must be different from the left one.");
            return;
        }
        p.sendMessage(ChatColor.GREEN + "Prison right Chest logged," + ChatColor.GOLD + " your prison is now operational.");
    }

    public void RemovePrison(Player p) {
        PH = new PrisonHandler(plugin);
        PH.RemovePrison(p.getName());
    }

    public Location PrisonLocation(String p) {
        PH = new PrisonHandler(plugin);
        return PH.GetPrisonLocation(p);
    }

    public boolean HasPrison(String p) {
        PH = new PrisonHandler(plugin);
        return PH.HasPrison(p);
    }

    public Location SignLocation(String p) {
        PH = new PrisonHandler(plugin);
        return PH.GetSign(p);
    }

    public Location ChestLLocation(String p) {
        PH = new PrisonHandler(plugin);
        return PH.GetChestL(p);
    }

    public Location ChestRLocation(String p) {
        PH = new PrisonHandler(plugin);
        return PH.GetChestR(p);
    }

    public void fillChest(final String p, final Player badguy) {
        PH = new PrisonHandler(plugin);

        BlockState stateL = PH.GetChestL(p).getBlock().getState();
        BlockState stateR = PH.GetChestR(p).getBlock().getState();
        Chest RBountyChest = (Chest) stateL;
        Chest LBountyChest = (Chest) stateR;

        ItemStack[] Bounties = badguy.getInventory().getContents();
        int Bslot = 0;
        int Lslot = 0;
        int Rslot = 0;
        while (Lslot <= LBountyChest.getInventory().getSize()) {
            if (Lslot <= LBountyChest.getInventory().getSize()) {
                if (Bslot < Bounties.length) {
                    if (Bounties[Bslot] == null) {
                        Bslot++;
                        continue;
                    } else {
                        LBountyChest.getInventory().addItem(Bounties[Bslot]);
                        Bslot++;
                        Lslot++;
                    }
                    LBountyChest.update(true);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        Bslot--;
        while (Rslot <= RBountyChest.getInventory().getSize()) {
            if (Rslot <= RBountyChest.getInventory().getSize()) {
                if (Bslot < Bounties.length) {
                    if (Bounties[Bslot] == null) {
                        Bslot++;
                        continue;
                    } else {
                        RBountyChest.getInventory().addItem(Bounties[Bslot]);
                        Bslot++;
                        Rslot++;
                    }
                    RBountyChest.update(true);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        badguy.getInventory().clear();
        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            public void run() {
                PH = new PrisonHandler(plugin);
                Sign poleSign = (Sign) PH.GetSign(p).getBlock().getState();
                poleSign.setLine(0, "BountyChest");
                poleSign.setLine(1, "Good: " + p);
                poleSign.setLine(2, "Bad: " + badguy.getName());
                poleSign.setLine(3, "");
                poleSign.update();
            }
        });
    }
}
